package com.borrowed.book.book.service.impl;

import com.borrowed.book.book.domain.*;
import com.borrowed.book.book.mapper.SOrderEvaluteImageMapper;
import com.borrowed.book.book.mapper.SOrderEvaluteMapper;
import com.borrowed.book.book.mapper.SPayOrderMapper;
import com.borrowed.book.book.service.EvaluteService;
import com.borrowed.book.user.domain.SUser;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class EvaluteServiceImpl implements EvaluteService {
    @Autowired
    private SOrderEvaluteMapper evaluteMapper;
    @Autowired
    private SOrderEvaluteImageMapper evaluteImageMapper;
    @Autowired
    private SPayOrderMapper payOrderMapper;
    /**
     * 添加备注
     *
     * @param param
     */
    @Override
    public void addOrderEvalute(AddEvaluteInDto param) {
        //更新订单，标记改订单已经评论过了
        Map<String,Object> map=new HashMap<>();
        map.put("orderNo",param.getOrderNo());
        map.put("evaluteFlag","1");
        payOrderMapper.updatePayOrderEvalute(map);
        SUser sysUser = (SUser) SecurityUtils.getSubject().getPrincipal();
        for(SOrderEvalute evalute:param.getData()) {
            evalute.setCreateTime(new Date());
            evalute.setOpenid(sysUser.getOpenid());
            evalute.setEvaluteStatus("1");
            evaluteMapper.insert(evalute);//保存书本的评论
            for (SOrderEvaluteImage image : evalute.getData()) {
                image.setEvaluteId(evalute.getId());
                image.setEvaluteStatus("1");
                evaluteImageMapper.insert(image);//保存评论下的图片信息
            }
        }
    }

    /**
     * 更新评论状态
     *
     * @param param
     */
    @Override
    public void updateEvaluteStatus(UpdEvaluteStatusInDto param) {
        evaluteMapper.updateEvaluteStatus(param);
    }

    /**
     * 更新评论图片的状态
     *
     * @param param
     */
    @Override
    public void updateEvaluteImageStatus(UpdEvaluteStatusInDto param) {
        evaluteImageMapper.updateEvaluteImageStatus(param);
    }

    /**
     * 评论一览查询
     *
     * @param param
     * @return
     */
    @Override
    public List<SOrderEvalute> selectOrderEvalute(SelectEvaluteInDto param) {

        List<SOrderEvalute> list=evaluteMapper.selectEvaluteList(param);
        for(SOrderEvalute evalute:list){
            List<SOrderEvaluteImage> data=evaluteImageMapper.selectEvaluteImageList(evalute.getId());
            evalute.setData(data);
        }
        return list;
    }
}
